//
//  YLMyInvitationController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/21.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLMyInvitationController.h"
#import "YLIntegralRecordCell.h"
#import "MYMyInvitationHeaderView.h"
#import "YLInvitingFriendsViewModel.h"
#import "YLInvitingFriendsModel.h"
@interface YLMyInvitationController ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeightConstraint;
@property (weak, nonatomic) IBOutlet UILabel *numbelLabel;

@property (nonatomic, strong) YLInvitingFriendsViewModel *viewModel;


@end

@implementation YLMyInvitationController
- (YLInvitingFriendsViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLInvitingFriendsViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _scrollView.alwaysBounceVertical = YES;
    
    self.title = @"我的邀请";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLIntegralRecordCell class]) bundle:nil] forCellReuseIdentifier:[YLIntegralRecordCell cellID]];
    
    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:nil] subscribeNext:^(id  _Nullable x) {
        weakSelf.numbelLabel.text = [NSString stringWithFormat:@"%zd", weakSelf.viewModel.item.item.count];
        [weakSelf.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = 0;
    if (self.viewModel.item) {
        count = self.viewModel.item.item.count;
    }
    _tableViewHeightConstraint.constant = count * 25 + 35;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLIntegralRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLIntegralRecordCell cellID]];
    cell.friendsModel = self.viewModel.item.item[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    MYMyInvitationHeaderView *header = [[MYMyInvitationHeaderView alloc] init];
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 35;
}

@end
